package com.cskaoyan.demo5.servlet;

import com.cskaoyan.demo5.bean.PageParameter;
import com.cskaoyan.demo5.common.CommonServlet;
import com.cskaoyan.demo5.mapper.MarketUserMapper;
import com.cskaoyan.demo5.model.MarketUser;
import com.cskaoyan.demo5.util.JacksonUtil;
import com.cskaoyan.demo5.util.MyBatisUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.RequestDispatcher;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;


/**
 * GET http://localhost:8080/demo5/admin/user/list?page=1&limit=20&sort=add_time&order=desc HTTP/1.1
 *
 * 拿到请求URL之后的请求参数，然后做查询
 * page和limit是做分页的：page是页码，limit是数据量  offset= (page - 1) * limit
 * sort和order做排序的：sort是排序的字段，order是排序的方式
 * 先排序在分页：SQL语句中既有分页语句又有排序语句的情况下
 */
@WebServlet("/admin/user/*")
public class AdminUserServlet extends CommonServlet {
    public void list(HttpServletRequest request, HttpServletResponse response) {
        // key=value形式的请求参数
        // page=1&limit=20&sort=add_time&order=desc
        String pageStr = request.getParameter("page");
        String limitStr = request.getParameter("limit");
        //转化类型
        int page = Integer.valueOf(pageStr);
        int limit = Integer.valueOf(limitStr);

        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        int offset=(page-1)*limit;
        List<MarketUser> list =
                mapper.list(offset, limit, sort, order);
        String jsonStr = JacksonUtil.write(list);
        System.out.println(jsonStr);
    }


    //http://localhost:8080/demo5/admin/user/list2?page=1&limit=20&sort=add_time&order=desc HTTP/1.1
    /**
     * 上面的参数接收过程还是比较繁琐的，并且获取的值为字符串，如果需要int或一些其他类型的值还需要手动转换
     * 1. 可以自己写反射工具
     * 2. 也可以使用现成的工具 → BeanUtils  Map → 对象
     *                                   map的key对应对象的成员变量
     */
    public void list2(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        // key=value形式的请求参数
        // page=1&limit=20&sort=add_time&order=desc

        // 把第二个参数的属性赋值给第一个参数
        // instance中要封装page、limit、sort、order
        PageParameter instance = new PageParameter();
        Map<String, String[]> parameterMap = request.getParameterMap();

        BeanUtils.copyProperties(instance,parameterMap);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        int offset=(instance.getPage()-1)*instance.getLimit();
        instance.setOffset((offset));
        List<MarketUser> list = mapper.list2(instance);
        String jsonStr = JacksonUtil.write(list);
        System.out.println(jsonStr);
    }
}
